
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;

using AntDesign.ProLayout;

using ZFY.Core;
using System.IO;
using ZFY.UMC.BlazorWebAssembly.HttpServices.IdentityHttpServices;
using ZFY.UMC.BlazorWebAssembly.HttpServices;

namespace ZFY.UMC.BlazorWebAssembly
{
public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");
            var http = new HttpClient()
            {
                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress),
                
            };
            
            http.DefaultRequestHeaders.AcceptCharset.Clear();
            http.DefaultRequestHeaders.AcceptCharset.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("utf-8"));
            http.DefaultRequestHeaders.AcceptEncoding.Add(System.Net.Http.Headers.StringWithQualityHeaderValue.Parse("utf-8"));
            http.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");

            builder.Services.AddSingleton(sp => http);



            using var response = await http.GetAsync("apiSettings.json");

            using var stream = await response.Content.ReadAsStreamAsync();


            builder.Configuration.AddJsonStream(stream);

            var url = builder.Configuration.GetSection("IdentityApi:BaseAddress");

            Console.WriteLine(url.Value);
            builder.Services.AddScoped<IdentityApiHttpMessageHandler>();
            //builder.Services
            //    .AddHttpClient<IdentityApiHttpClient>(client => client.BaseAddress = new Uri(url.Value))
            //    .AddHttpMessageHandler<IdentityApiHttpMessageHandler>();

            builder.Services.AddHttpClient("IdentityApi", client => client.BaseAddress = new Uri(url.Value)).AddHttpMessageHandler<IdentityApiHttpMessageHandler>();

            builder.Services.AddSingleton<IIdentityUserService,IdentityUserService>();
            builder.Services.AddSingleton<CurrentUserProfile>();
            builder.Services.AddSingleton<CommonService>();
            builder.Services.AddHttpContextAccessor();

            builder.Services.AddOidcAuthentication(options =>
            {
                // Configure your authentication provider options here.
                // For more information, see https://aka.ms/blazor-standalone-auth
                builder.Configuration.Bind("oidc", options.ProviderOptions);

                
                
            });
            

            builder.Services.AddAntDesign();
            builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));

            await builder.Build().RunAsync();
        }
    }
}